package com.stone;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Spider {

	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		/*
		for (int j = 1; j <= 327; j++) {
			storeItem(j,35,"xiuxian");
		}
		for (int j = 1; j <= 166; j++) {
			storeItem(j,36,"yizhi");
		}
		for (int j = 1; j <= 23; j++) {
			storeItem(j,37,"jsby");
		}
		for (int j = 1; j <= 19; j++) {
			storeItem(j,38,"zl");
		}
		for (int j = 1; j <= 58; j++) {
			storeItem(j,39,"dz");
		}
		for (int j = 1; j <= 52; j++) {
			storeItem(j,40,"sj");
		}
		for (int j = 1; j <= 9; j++) {
			storeItem(j,41,"jy");
		}
		for (int j = 1; j <= 4; j++) {
			storeItem(j,42,"yc");
		}
		for (int j = 1; j <= 16; j++) {
			storeItem(j,43,"mx");
		}
		for (int j = 1; j <= 4; j++) {
			storeItem(j,44,"wy");
		}
		for (int j = 1; j <= 26; j++) {
			storeItem(j,45,"qp");
		}
		for (int j = 1; j <= 6; j++) {
			storeItem(j,101,"mlq");
		}*/
		for (int j = 1; j <= 21; j++) {
			storeItem(j,121,"ty");
		}
		
		
		/*
		  String url = "<title>水果忍者官方版-Fruit Ninja安卓版android-休闲游戏免费下载-机锋市场</title>"; Pattern
		  pattern = Pattern.compile("^<title>.*游戏.*</title>$");
		  Matcher matcher =pattern.matcher(url);
		  boolean result = matcher.find();
		 System.out.println(result);*/
		 
		/*String url = "<title>水果忍者官方版-Fruit Ninja安卓版android-休闲游戏免费下载-机锋市场</title>";
		Pattern pa = Pattern.compile("i");
		Matcher ma = pa.matcher(url);
		boolean result = ma.find();
		while(result) {
			for (int i = 0; i <= ma.groupCount(); i++) {
				System.out.println(ma.group(i));
			}
            //继续查找下一个匹配对象
            result = ma.find();
        }
		*/
		

	}
	
	
	
	private static void storeItem(int j,int k,String title) {
		// TODO Auto-generated method stub
		String url = "http://apk.gfan.com/Aspx/UserApp/softpotal.aspx?softCategory="+k+"&orderBy=DownNum&i=3&CurrPage="+j;
		String file = getFile(url);

		Pattern pa = Pattern.compile("\"/Product/App[0-9]*\\.html\"");
		Matcher ma = pa.matcher(file);
		boolean result = ma.find();
		StringBuffer str = new StringBuffer();
		while(result) {
			for (int i = 0; i <= ma.groupCount(); i++) {
				str.append(ma.group(i)+"\n");
			}
            //继续查找下一个匹配对象
            result = ma.find();
        }
		
		try {
			File ff = new File(title);
			if(!ff.exists()){
				ff.mkdirs();
			}
			File f = new File(title+"/wenjian"+j);
			 BufferedWriter output = new BufferedWriter(new FileWriter(f));
		     output.write(str.toString());
		     output.close();
		     
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}



	private static String getFile(String urlString) {
		// TODO Auto-generated method stub
		StringBuffer buffer = new StringBuffer();
		boolean flag = false;
		try {
			URL url = new URL(urlString);
			URLConnection conn = url.openConnection();

			BufferedReader is = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			String str;
			while ((str = is.readLine()) != null) {
				buffer.append(str);
				/*Pattern pattern = Pattern.compile("^<title>.*游戏.*</title>$");
				Matcher matcher = pattern.matcher(str);
				flag = matcher.find();*/
			}
			is.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return buffer.toString();
		
	}
	
	/*buffer.append(str);
	Pattern pattern = Pattern.compile("^<title>.*游戏.*</title>$");
	Matcher matcher = pattern.matcher(str);
	boolean flag = matcher.find();
	//如果是游戏，开始处理，
	if (flag) {
		//处理成json数据
		
	}*/
	
}
